package com.example.cleantest;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.my.ads.lib.AdSummary;
import com.my.ads.lib.Config;
import com.rapid.clean.deep.R;

/**
 * Created by xuanyang on 2018/3/8.
 */

public class JunkCleanActivity extends Activity {

    private FrameLayout back;
    private ImageView img_clean_bg, img_clean_bg_1;
    private RelativeLayout relative_ad_26, relative_ad_38;


    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_junk_clean);
        Log.e("dsa","cc");
        initViews();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                startActivity();
            }
        });

        start();


        showNativeAd();
        Log.e("aa","cc");
        if (!TextUtils.isEmpty(getIntent().getStringExtra("time"))) {
//            ActiveIcon.setSp(JunkCleanActivity.this, "xq_sp", "time", getIntent().getStringExtra("time"));
        }
    }

    private void showNativeAd() {
        Log.e("sda","nn");
        try {
            AdSummary.getInstance().showBannerAd(relative_ad_26);
//            FacebookNative.getInstance().showSmallFbNativeAd(this, relative_ad_38);
        } catch (Exception e) {
        }
    }

    private void start() {
        startAnimation(1);
    }

    @Override
    public void onResume() {
        super.onResume();
        resumeAd();


    }

    private void resumeAd() {
        Log.e("dsa","11");
        SharedPreferences sp_3 = getSharedPreferences("banner_ad", Context.MODE_PRIVATE);
        Long show_time_ad_3 = sp_3.getLong("show_banner_ad_time", 0L);

        if (show_time_ad_3 != 0L && (System.currentTimeMillis() - show_time_ad_3 >= (Integer.valueOf(Config.update_native_ad) * 1000))) {
            AdSummary.getInstance().showBannerAd(relative_ad_26);
        }
    }

    private void startAnimation(int useless) {

        final RotateAnimation animation = new RotateAnimation(0f, 1440f, Animation.RELATIVE_TO_SELF,
                0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(4000);//设置动画持续时间
        animation.setFillAfter(true);
        animation.setFillBefore(false);
        img_clean_bg.startAnimation(animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                otherAnimation();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }

    private void startActivity() {
        Intent intent = new Intent(JunkCleanActivity.this, JunkCleanResultActivity.class);
        startActivity(intent);
    }

    private void otherAnimation() {
        TranslateAnimation translateAnimation = new TranslateAnimation(0.0f, 0.0f, 10.0f, -1200.60f);
        translateAnimation.setDuration(1200);
        //设置动画结束之后的状态是否是动画的最终状态，true，表示是保持动画结束时的最终状态
        translateAnimation.setFillAfter(true);
        //设置动画结束之后的状态是否是动画开始时的状态，true，表示是保持动画开始时的状态
        translateAnimation.setFillBefore(false);
        img_clean_bg_1.startAnimation(translateAnimation);
        translateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                finish();
                startActivity();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });


    }

    private void initViews() {
        back = (FrameLayout) findViewById(R.id.back);
        img_clean_bg = (ImageView) findViewById(R.id.img_clean_bg);
        relative_ad_26 = (RelativeLayout) findViewById(R.id.relative_ad_26);
        relative_ad_38 = (RelativeLayout) findViewById(R.id.relative_ad_38);
        img_clean_bg_1 = (ImageView) findViewById(R.id.img_clean_bg_1);
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        startActivity();
    }


}
